﻿using System;
using System.Windows;

namespace HeritageAcademy.Library.Controls
{
	/// <summary>
	/// Course statistics table class.
	/// </summary>
	public partial class StatTable
    {
        #region Ctor
        public StatTable()
		{
			InitializeComponent();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Add row to table.
        /// </summary>
        /// <param name="row">Object of TableRow class</param>
        public void AddTableRow(StatTableRow row)
        {
            // it's a hack to align verical lines
            row.Margin = new Thickness(-1, 0, 0, 0);
            // add row item
            lbCourseStat.Items.Add(row);
        }

        /// <summary>
        /// Clear table.
        /// </summary>
        public void ClearTable()
        {
            lbCourseStat.Items.Clear();
        }

        /// <summary>
        /// Calculate the total percent of progress.
        /// </summary>
        /// <returns>String of rounded percent value</returns>
        public string GetTotalPercent()
        {
            var p = 0.0;
            var total = lbCourseStat.Items.Count;

            for (var i = 0; i < total; i++)
                p += ((StatTableRow) lbCourseStat.Items[i]).BarPercent;
            
            return Math.Round((p * 100)/total).ToString();
        }
        #endregion
    }
}